package com.ysd.lis.service.impl;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ysd.common.Result;
import com.ysd.lis.entity.WkQualityDetail;
import com.ysd.lis.entity.WkQualityPlan;
import com.ysd.lis.entity.WkQualityPlan;
import com.ysd.lis.entity.WkQualityStage;
import com.ysd.lis.mapper.WkQualityDetailMapper;
import com.ysd.lis.mapper.WkQualityPlanMapper;
import com.ysd.lis.mapper.WkQualityStageMapper;
import com.ysd.lis.request.QualityPlanListParams;
import com.ysd.lis.service.WkQualityPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 质量控制计划表 服务实现类
 * </p>
 *
 * @author LiuQi
 * @since 2024-05-06
 */
@Service
public class WkQualityPlanServiceImpl extends ServiceImpl<WkQualityPlanMapper, WkQualityPlan> implements WkQualityPlanService {

    @Autowired
    WkQualityPlanMapper wkQualityPlanMapper;
    @Autowired
    WkQualityDetailMapper wkQualityDetailMapper;
    @Autowired
    WkQualityStageMapper wkQualityStageMapper;

    @Override
    public Result getQualityPlanList(QualityPlanListParams qualityPlanListParams) {
        LambdaQueryWrapper<WkQualityPlan> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(WkQualityPlan::getDelFlag, 0)
                .eq(WkQualityPlan::getPlanStatus, qualityPlanListParams.getStatus())
                .eq(StrUtil.isNotEmpty(qualityPlanListParams.getPlanTypeId()), WkQualityPlan::getPlanTypeId, qualityPlanListParams.getPlanTypeId())
                .eq(!qualityPlanListParams.getIsAll(), WkQualityPlan::getLanchUserId, qualityPlanListParams.getUserId());
        List<WkQualityPlan> wkQualityPlans = wkQualityPlanMapper.selectList(lambdaQueryWrapper);
        return Result.succ(1, "查询成功！", wkQualityPlans);
    }

    @Override
    public Result addQualityPlan(WkQualityPlan wkQualityPlan) {
        boolean save = this.save(wkQualityPlan);
        if (save) {
            return Result.succ(1, "插入成功！", save);
        } else {
            return Result.fail("插入失败！");
        }
    }

    @Override
    public Result editQualityPlan(WkQualityPlan wkQualityPlan) {
        boolean save = this.updateById(wkQualityPlan);
        if (save) {
            return Result.succ(1, "编辑成功！", save);
        } else {
            return Result.fail("编辑失败！");
        }
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Result deleteQualityPlan(List<String> ids) {
        boolean save = this.removeByIds(ids);
        // 删除计划明细
        LambdaQueryWrapper<WkQualityDetail> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.in(WkQualityDetail::getPlanId, ids);
        wkQualityDetailMapper.delete(lambdaQueryWrapper);
        LambdaQueryWrapper<WkQualityStage> lambdaQueryWrapper1 = new LambdaQueryWrapper<>();
        lambdaQueryWrapper1.in(WkQualityStage::getPlanId, ids);
        wkQualityStageMapper.delete(lambdaQueryWrapper1);
        if (save) {
            return Result.succ(1, "删除成功！", save);
        } else {
            return Result.fail("删除失败！");
        }
    }

    @Override
    public Result updatePlanStatus(Integer status, String id) {
        LambdaUpdateWrapper<WkQualityPlan> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(WkQualityPlan::getPlanStatus, status)
                .eq(WkQualityPlan::getId, id);
        int update = wkQualityPlanMapper.update(null, updateWrapper);
        if (update == 0) {
            return Result.succ(1, "无更新数据", "");
        } else if (update > 0) {
            return Result.succ(1, "更新成功", "");
        } else {
            return Result.fail("更新失败");
        }
    }
}
